package com.example.demo.controller;

import com.example.demo.pojo.Customer;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.Staff;
import com.example.demo.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StaffController {

    @Autowired
    private StaffService staffService;

    @PostMapping("/login")
    public Result login(@RequestBody Staff staff) {
        // 提取用户名和密码
        String username = staff.getUsername();
        String password = staff.getPassword();
        Staff bystaff = staffService.selectStaffByusername(username);
        if (bystaff == null){
            // 如果用户名不存在，则返回用户名错误信息
            return Result.error("用户名错误");
        }
        if (password.equals(bystaff.getPassword())){
            // // 如果密码匹配，则登录成功
            return Result.success(bystaff);
        }
        // 如果密码不匹配，则返回密码错误信息
        return Result.error("密码错误！");
    }

    @GetMapping("/login/list")
    public List<Staff> selectAllStaffs() {
        return staffService.selectAllStaffs();
    }

    @PostMapping("/staff/search")
    public Staff searchStaffById(@RequestBody Staff staff) {
        int staff_id = staff.getStaff_id();
        Staff staff1= staffService.selectStaffById(staff_id);
        if (staff1 != null){
            return staff1;
        }
        return null;
    }

    @PostMapping("/staff/add")
    public Result addStaff(@RequestBody Staff staff) {
        if (staff != null){
            staffService.addStaff(staff);
            return Result.success();
        }else{
            return Result.error("添加失败");
        }
    }

    @PutMapping("/staff/update/{id}")
    public Result updateStaff(@PathVariable int id, @RequestBody Staff staff) {
        if (id >= 10000 && id<200000){
            staffService.updateStaff(staff);
            return Result.success();
        }else{
            return Result.error("更新失败");
        }
    }
    @DeleteMapping("/staff/delete/{id}")
    public Result delete(@PathVariable int id) {
        if (id >= 10000 && id<200000){
            staffService.deleteStaff(id);
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }
    @PutMapping("/staff/import")
    public Result changeAllStaffs(@RequestBody List<Staff> staffs) {
        if (staffs != null){
            staffService.changeAllStaffs(staffs);
            return Result.success();
        }else{
            return Result.error("导入失败");
        }
    }

    @GetMapping("/staff/staff_num")
    public Result staff_num() {
        int num = staffService.staff_num();
        if (num != 0){
            staffService.staff_num();
            return Result.success(num);
        }
        return Result.error("获取总员工数失败");
    }
}
